package com.fjrz.bbxwj.main.host.side

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.app.RZApplication
import com.fjrz.bbxwj.main.access.AccessActivity
import com.fjrz.bbxwj.main.host.MainActivity
import com.fjrz.bbxwj.mode.manager.getDiskCacheDirectory
import com.fjrz.bbxwj.mode.vm.UserViewModel
import com.module.lemlin.view.DialogHintPopup
import com.fjrz.bbxwj.popup.PasswordSettingPopup
import com.module.lemlin.view.LoadingDialog
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.code
import com.module.lemlin.rxhttp.msg
import kotlinx.android.synthetic.main.activity_main_setting.*
import java.lang.StringBuilder

class MainSettingActivity : ViewModeActivity<UserViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MainSettingActivity::class.java)
            activity.startActivity(intent)
        }
    }


    private val cacheHintPopup: DialogHintPopup by lazy {
        DialogHintPopup(context = this,
            onConfirm = {
                cacheHintPopup.dismiss()
            },
            onCancel = {
                cacheHintPopup.dismiss()
            })
    }

    private val exitHintPopup: DialogHintPopup by lazy {
        DialogHintPopup(context = this,
            onConfirm = {
                exitHintPopup.dismiss()
                SPStaticUtils.clear()
                ActivityUtils.finishActivity(MainSettingActivity::class.java)
                ActivityUtils.finishActivity(MainActivity::class.java)
                AccessActivity.open(activity = this)
            },
            onCancel = {
                exitHintPopup.dismiss()
            })
    }

    private val pwdSettingPopup: PasswordSettingPopup by lazy {
        PasswordSettingPopup(context = this, onGetCode = { phone ->
            //获取验证码
            viewModel.userSmsSend(phone, event = "set_pwd")
        }, onConfirm = { smsCode, password, confirmPassword ->
            viewModel.userSetPwd(smsCode, password, confirmPassword)
        })
    }


    override fun viewModel(): UserViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_main_setting

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, view = flSettingBar)
    }

    override fun initView() {

        RZApplication.userInfoMe()?.password_set?.let {
            tvSettingPwdStatus.text = if (it != 1) "去设置" else "修改"
        }
        tvSettingFileSize.text =
            FileUtils.getDirSize(getDiskCacheDirectory(this as Context))
        tvSettingVersion.text =
            StringBuilder().append("V").append(AppUtils.getAppVersionName())

        ivSettingBack.setOnClickListener {
            finish()
        }

        flSettingCache.setOnClickListener {
            cacheHintPopup.setTextTitle("温馨提示")
                .setTextMessage("是清除缓存数据？")
                .setTextCancel("否")
                .setTextConfirm("是")
                .showPopupWindow()
        }

        flSettingPassword.setOnClickListener {
            pwdSettingPopup.setPhone(RZApplication.userInfoMe()?.mobile)
                .setTitle("访问密码设置")
                .showPopupWindow()
        }

        tvSettingAbout.setOnClickListener {
            MainRichTextActivity.open(activity = this, title = "关于我们", name = "about_us_text")
        }

        tvSettingPactUser.setOnClickListener {
            MainRichTextActivity.open(activity = this, title = "用户协议", name = "user_agreement_text")
        }

        tvSettingPactPrivate.setOnClickListener {
            MainRichTextActivity.open(activity = this, title = "隐私协议", name = "privacy_policy_text")
        }

        tvSettingProblem.setOnClickListener {
            MainRichTextActivity.open(activity = this, title = "常见问题", name = "common_problem_text")
        }

        tvSettingExit.setOnClickListener {
            exitHintPopup.setText(
                textTitle = "提示",
                textMessage = "是否确定退出登录",
                textConfirm = "确定",
                textCancel = "取消"
            ).showPopupWindow()
        }
    }

    override fun initViewMode() {


        viewModel.userSmsLiveData.observe(this as LifecycleOwner, Observer { response ->
            when (response.status) {
                Status.START -> if (pwdSettingPopup.isShowing)
                    pwdSettingPopup.startCodeDownTime()
                Status.FAILURE -> {
                    toast(response.throwable?.code,response.throwable?.msg)
                }
                Status.SUCCESS -> response.httpData?.let {
                    toast(message = it.msg ?: "发送成功")
                }
            }
        })


        viewModel.userSetPwdLiveData.observe(this as LifecycleOwner, Observer { response ->
            if (responseStatus2LiveData(response.status, response.throwable) != Status.SUCCESS)
                return@Observer
            response.httpData?.let {
                toast(message = it.msg ?: "设置成功")
                pwdSettingPopup.dismiss()
            }
        })
    }


    private fun responseStatus2LiveData(status: Status, throwable: Throwable?): Status {
        if (status == Status.START) LoadingDialog.show(
            context = this, resId = R.layout.dialog_load_style_1
        ) else if (status == Status.FAILURE) {
            LoadingDialog.dismiss()
            toast(throwable?.code,throwable?.msg)
        } else {
            LoadingDialog.dismiss()
        }
        return status
    }
}